package studentManagement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @Author admin
 * @Date 2019/4/30 14:44
 */
public class StudentDaoDB implements StudentDao {
    Connection conn = JDBCUtils.getConnection();
    PreparedStatement pstmt = null;
    ResultSet rs=null;


    @Override
    public int insert(Student student) {
        if (query(student.getName())!=null){
            String sql="insert into book(id,name,age,sex,className) values(?,?,?,?,?)";
            try {
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,student.getId());
                pstmt.setString(2,student.getName());
                pstmt.setInt(3,student.getAge());
                pstmt.setString(4,student.getSex());
                pstmt.setString(5,student.getClassName());
                int c = pstmt.executeUpdate();
                return c;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public Student query(String id) {
        String sql="select * from student where id=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,id);
            rs=pstmt.executeQuery();
            /* 因为图书名称不能重复，所以结果集中只查询出一条语句 */
            if (rs.next()){
                /* 结果集中的内容 返回*/
                String id1=rs.getString("id");
                String name=rs.getString("name");
                int age=rs.getInt("age");
                String sex=rs.getString("sex");
                String className=rs.getString("className");
                Student student=new Student(id,name,age,sex,className);
                return student;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void select() {


    }

    @Override
    public ArrayList<Student> selectAll() {
        ArrayList<Student> arrayList = new ArrayList<Student>();
        String sql="select * from student";
        try {
            pstmt = conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                String id = rs.getString("id");
                String name =rs.getString("name");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String className = rs.getString("className");
                Student student = new Student(id,name,age,sex,className);
                arrayList.add(student);
            }
            return arrayList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Student s) {
        String sql="update student set name=?,sex=?,age=?,className=? where id=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,s.getName());
            pstmt.setString(2,s.getSex());
            pstmt.setInt(3,s.getAge());
            pstmt.setString(4,s.getClassName());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Student student) {
        return 0;
    }

    @Override
    public void exit() {
        System.exit(0);
    }
}
